# -*- coding: utf-8 -*-
"""
oyTransferShaders by E. Ozgur Yilmaz (c) 2010

v10.10.7

Description :
-------------
a simple script for transfering shaders from source to target object

Version History :
-----------------

v10.10.7
- initial working version
"""



__version__ = "10.10.7"



import pymel.core as pm



def transferShaders( source, target):
    
    sourceShape = source.getShape()
    targetShape = target.getShape()
    
    # get the shadingEngines
    shadingEngines = [ shEn for shEn in source.getShape().inputs() if isinstance( shEn, pm.nodetypes.ShadingEngine)]
    
    dataStorage = []
    
    # get the assigned faces
    for shadingEngine in shadingEngines:
        faces = pm.sets(shadingEngine, q=1)
        for faceGroup in faces:
            strFace = str(faceGroup)
            # replace the objectName
            newFace = strFace.replace( sourceShape.name(), targetShape.name() )
            dataStorage.append( (shadingEngine.name(), newFace) )
    
    for data in dataStorage:
        shadingEngine = data[0]
        newFace = data[1]
        pm.select(newFace)
        # now assign the newFaces to the set
        pm.sets(shadingEngine, fe=1)